package com.dianrui.operation.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.dianrui.operation.R;
import com.dianrui.operation.util.StringUtils;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 显示位置
 */
public class ShowLocationsActivity extends BaseActivity {
    private AMap aMap;
    private String lat,lng,status;
    private String showtitles;
    private Marker marker;
    private String carNumber;
    private String mDegree;
    private String mFormName;
    private Marker clickMaker;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.mapviews)
    MapView mapviews;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapviews.onCreate(savedInstanceState);
    }

    @Override
    public int getContentViewId() {
        return R.layout.show_locations;
    }

    @Override
    public void initView() {
        title.setText("查看位置");
        if (aMap == null) {
            aMap = mapviews.getMap();
        }
        UiSettings mUiSettings = aMap.getUiSettings();
        mUiSettings.setTiltGesturesEnabled(false);
        mUiSettings.setRotateGesturesEnabled(false);
        mUiSettings.setZoomControlsEnabled(false);
        if (getIntent() != null &&getIntent().getExtras()!=null) {
            lat=getIntent().getStringExtra("lat");
            lng=getIntent().getStringExtra("lng");
            status=getIntent().getStringExtra("status");
            showtitles=getIntent().getStringExtra("title");
            carNumber=getIntent().getStringExtra("number");
            mDegree=getIntent().getStringExtra("degree");
            mFormName = getIntent().getStringExtra("formname");
        }
        aMap.setOnMarkerClickListener(markerClickListener);
        aMap.setInfoWindowAdapter(infoWindowAdapter);
        aMap.setOnInfoWindowClickListener(listener);
        initLocations();
        aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                if (clickMaker != null && clickMaker.isInfoWindowShown()) {
                    clickMaker.hideInfoWindow();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapviews.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapviews.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapviews.onSaveInstanceState(outState);
    }


    @Override
    public void initData() {

    }

    private void initLocations() {
        if ("0".equals(mDegree)) {
            if (!StringUtils.isEmpty(lat)&&!StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationsActivity.this,R.layout.trouble_point, null);
                LatLng latLng=new LatLng(Double.valueOf(lat),Double.valueOf(lng));
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));
                aMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromView(view)).setFlat(true));

            }

        }else {
            if (!StringUtils.isEmpty(lat)&&!StringUtils.isEmpty(lng)) {
                View view = View.inflate(ShowLocationsActivity.this, R.layout.trouble_point, null);
                LatLng latLng = new LatLng(Double.valueOf(lat), Double.valueOf(lng));
                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));
                aMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromView(view)).setFlat(true));

            }
        }
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }


    // 定义 Marker 点击事件监听
    AMap.OnMarkerClickListener markerClickListener = new AMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(final Marker marker) {
            clickMaker = marker;
            marker.showInfoWindow();
            return true;
        }
    };

    //显示infowindow
    AMap.InfoWindowAdapter infoWindowAdapter=new AMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            View view = LayoutInflater.from(ShowLocationsActivity.this).inflate(R.layout.show_location_point_bubble, null);
            TextView showNumbers =view.findViewById(R.id.number);
            TextView showTitle =view.findViewById(R.id.titles);
            showNumbers.setText(carNumber);
            showTitle.setText(mFormName);
            return view;
        }

        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }
    };

    //点击marker就消失
    AMap.OnInfoWindowClickListener listener = new AMap.OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {
            marker.hideInfoWindow();
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapviews.onDestroy();
    }
}
